<?php
namespace app\admin\controller;
use think\Db;
use think\Validate;
class Service
{
	public function __construct(){
		Admin_auth();
	}
	
    public function index()
    {
        $edit = request()->request('edit');
        if($edit == 'sort'){
            $id = request()->request('id');
            $sort = (int)request()->request('sort');
            Db::table('service')->where('id',$id)->update(['sort'=>$sort]);
        }else{
            $gid = input('get.gid');
            $cid = input('get.cid');
            $uid = input('get.uid');
            $title = input('get.title');
            if(!empty($cid)){
                $where = ['status'=>['BETWEEN','1,2'],'cid'=>$cid];
            }else if(!empty($gid)){
                $where = ['status'=>['BETWEEN','1,2'],'gid'=>$gid];
            }else if(!empty($uid)){
                $where = ['status'=>['BETWEEN','1,2'],'uid'=>$uid];
            }else{
                $where = ['status'=>['BETWEEN','1,2']];
            }
            if(!empty($title)){
                $where['title'] = ['like','%'.$title.'%'];
            }
            $pages = pages('service',$where,input('get.page'));
            $data = Db::table("service")->where($where)->whereIn('pid',session('wmo2o_a_pid'))->limit($pages['limit'])->order("replace(flag,'0','desc'),id desc")->select();
            return view('',['data'  => $data,'pages' => $pages['pages']]);
        }
    }
	
	public function edit()
    {
		$id = input("get.id")?input("get.id"):0;
		if(request()->isPost()){
			$data = input("post.");
			$validate = new Validate([
				['title','require',lang('label')['title'].lang('currency')['required']],
				['price','require',lang('label')['price'].lang('currency')['required']],
				['stock','require',lang('label')['stock'].lang('currency')['required']],
				['content','require',lang('label')['content'].lang('currency')['required']],
			]);
			if (!$validate->check($data)) {
				return error($validate->getError());
			}
			if(!empty($data['spec'])){
				$data['spec'] = json_encode($data['spec']);
			}
			if(!empty($data['attr'])){
				$data['attr'] = json_encode($data['attr']);
			}
			if($id){
				Db::table("service")->where(['id' => $id])->whereIn('pid',session('wmo2o_a_pid'))->update($data);
				$data['id'] = $id;
			}else{
                $data['sort'] = 0;
                $data['time'] = time();
				$data['id'] = Db::table("service")->insertGetId($data);
			}
		}
		$data = Db::table("service")->where(['id' => $id])->whereIn('pid',session('wmo2o_a_pid'))->find();
		if(!empty($data['spec'])){
			$data['spec'] = json_decode($data['spec'],true);
		}
		if(!empty($data['attr'])){
			$data['attr'] = json_decode($data['attr'],true);
		}
        return view('',['data'  => $data]);
    }
	
	public function del()
    {
		$id = input("get.id")?input("get.id"):0;
		$page = input("get.page");
		Db::table("service")->whereIn('pid',session('wmo2o_a_pid'))->delete($id);
		redirect(url('/admin/service?page='.$page));
		exit;
    }
	
	public function flag()
    {
		$id = input("get.id");
		$flag = input("get.flag");
		$flagtime = input("get.flagtime");
		if(!$id){
			$return = array(
				'code' => 'error',
				'msg' => "id".lang('currency')['error']
			);
			return $return;
		}
		if($flagtime){
			$flagtime = strtotime($flagtime)+86399;
		}
		$update['flag'] = $flag;
		$update['flagtime'] = $flagtime;
		$data = Db::table("service")->where(['id' => $id])->whereIn('pid',session('wmo2o_a_pid'))->update($update);
		$return = array(
			'code' => 'success',
			'msg' => lang('currency')['success']
		);
		return $return;
    }
	
	public function examine()
    {
		$pages = pages('service',['status'=>3],input('get.page'));
		$data = Db::table("service")->where(['status'=>3])->whereIn('pid',session('wmo2o_a_pid'))->limit($pages['limit'])->order('id desc')->select();
        return view('',['data'  => $data,'pages' => $pages['pages']]);
    }
	
	public function setting()
    {
		if(request()->isPost()){
			$data = input("post.");
			Db::table("seller_config")->where(['id'=>1])->update($data);
		}
		$data = Db::table("seller_config")->where(['id'=>1])->find();
        return view('',['data' => $data]);
    }
	
	public function category()
    {
        $edit = request()->request('edit');
        if($edit == 'sort'){
            $id = request()->request('id');
            $sort = (int)request()->request('sort');
            Db::table('service_category')->where('id',$id)->update(['sort'=>$sort]);
        }else {
            $data = array();
            $category = Db::table("service_category")->where('gid', 0)->order('id asc')->select();
            if (!empty($category)) {
                foreach ($category as $sub) {
                    $sub['sub'] = Db::table("service_category")->where('gid', $sub['id'])->order('id asc')->select();
                    Array_push($data, $sub);
                }
            }
            return view('', ['data' => $data]);
        }
    }
	
	public function category_edit()
    {
		$id = input("get.id")?input("get.id"):0;
		$data = array();
		if(request()->isPost()){
			$data = input("post.");
			if($id){
				Db::table("service_category")->where(['id' => $id])->update($data);
			}else{
			    $data['sort'] = 0;
				Db::table("service_category")->insert($data);
			}
			$return = array(
				'code' => 'success',
				'msg' => lang('currency')['success']
			);
			return $return;
		}  
    }
	
	public function category_del()
    {
		$id = input("get.id")?input("get.id"):0;
		Db::table("service_category")->delete($id);
		Db::table("service")->where('cid',$id)->delete();
		Db::table("service_category")->where('gid',$id)->delete();
		Db::table("service")->where('gid',$id)->delete();
		$return = array(
				'code' => 'success',
				'msg' => lang('currency')['success']
			);
		return $return;
    }
	
	public function ajax_get()
    {
		$gid = input('get.id')?input('get.id'):0;
		$data = Db::table("service_category")->where(['gid'=>$gid])->whereIn('id',session('wmo2o_a_category'))->order('id asc')->select();
		$return = array(
			'data' => $data
		);
		return $return;
    }
}
